package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Notice;
import cn.wolfcode.mapper.NoticeMapper;
import cn.wolfcode.qo.NoticeQueryObject;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class NoticeServiceImpl implements INoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    @Transactional
    public void save(Notice notice) {
        //创建用户
//        notice.setCreator(UserContext.getEmployee().getId());
        //创建时间
        notice.setCreationTime(new Date());
        noticeMapper.insert(notice);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        noticeMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void update(Notice notice) {
        noticeMapper.updateByPrimaryKey(notice);
    }

    @Override
    public Notice get(Long id) {
        return noticeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Notice> listAll() {
        return noticeMapper.selectAll();
    }

    @Override
    public PageInfo<Notice> query(NoticeQueryObject qo) {
        // 告诉插件我们查询的是第几页，每页多少条
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        // 查询当前页的数据
        List<Notice> notices = noticeMapper.selectForList(qo);
        return new PageInfo<>(notices);
    }

    @Override
    public void updateStatus(Long id, Integer postStatus) {
        noticeMapper.updateStatus(id,postStatus);
    }

    @Override
    public void show(Long id, Integer haveRead) {
        noticeMapper.show(id,haveRead);
    }
}
